home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_perl.idb / usr / freeware / catman / p_man / cat3 / Class::Struct.Z / Class::Struct
Encoding:
Text File  |  1998-10-28  |  12.0 KB  |  397 lines

  1.  
  2.  
  3.  
  4.      CCCCllllaaaassssssss::::::::SSSSttttrrrruuuucccctttt((((3333)))) 1111////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222)))) CCCCllllaaaassssssss::::::::SSSSttttrrrruuuucccctttt((((3333))))
  5.  
  6.  
  7.  
  8.      NNNNAAAAMMMMEEEE
  9.       Class::Struct    - declare struct-like datatypes    as Perl
  10.       classes
  11.  
  12.      SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  13.           use Class::Struct;
  14.               #    declare    struct,    based on array:
  15.           struct( CLASS_NAME => [ ELEMENT_NAME => ELEMENT_TYPE, ...    ]);
  16.               #    declare    struct,    based on hash:
  17.           struct( CLASS_NAME => { ELEMENT_NAME => ELEMENT_TYPE, ...    });
  18.  
  19.           package CLASS_NAME;
  20.           use Class::Struct;
  21.               #    declare    struct,    based on array,    implicit class name:
  22.           struct( ELEMENT_NAME => ELEMENT_TYPE, ...    );
  23.  
  24.           package Myobj;
  25.           use Class::Struct;
  26.               #    declare    struct with four types of elements:
  27.           struct( s    => '$',    a => '@', h => '%', c => 'My_Other_Class' );
  28.  
  29.           $obj = new Myobj;              #    constructor
  30.  
  31.                           #    scalar type accessor:
  32.           $element_value = $obj->s;          # element value
  33.           $obj->s('new value');          # assign to element
  34.  
  35.                           #    array type accessor:
  36.           $ary_ref = $obj->a;          # reference to whole array
  37.           $ary_element_value = $obj->a(2);      # array element value
  38.           $obj->a(2, 'new value');          # assign to array element
  39.  
  40.                           #    hash type accessor:
  41.           $hash_ref    = $obj->h;          # reference to whole hash
  42.           $hash_element_value = $obj->h('x'); # hash element value
  43.           $obj->h('x', 'new    value');    # assign to hash element
  44.  
  45.                           #    class type accessor:
  46.           $element_value = $obj->c;          # object reference
  47.           $obj->c->method(...);          # call method    of object
  48.           $obj->c(new My_Other_Class);      # assign a new object
  49.  
  50.  
  51.      DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  52.       Class::Struct    exports    a single function, struct.  Given a
  53.       list of element names    and types, and optionally a class
  54.       name,    struct creates a Perl 5    class that implements a
  55.       "struct-like"    data structure.
  56.  
  57.       The new class    is given a constructor method, new, for
  58.       creating struct objects.
  59.  
  60.  
  61.  
  62.  
  63.      Page 1                        (printed 10/23/98)
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.      CCCCllllaaaassssssss::::::::SSSSttttrrrruuuucccctttt((((3333)))) 1111////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222)))) CCCCllllaaaassssssss::::::::SSSSttttrrrruuuucccctttt((((3333))))
  71.  
  72.  
  73.  
  74.       Each element in the struct data has an accessor method,
  75.       which    is used    to assign to the element and to    fetch its
  76.       value.  The default accessor can be overridden by declaring
  77.       a sub    of the same name in the    package.  (See Example 2.)
  78.  
  79.       Each element's type can be scalar, array, hash, or class.
  80.  
  81.       TTTThhhheeee ssssttttrrrruuuucccctttt(((()))) function
  82.  
  83.       The struct function has three    forms of parameter-list.
  84.  
  85.           struct( CLASS_NAME => [ ELEMENT_LIST ]);
  86.           struct( CLASS_NAME => { ELEMENT_LIST });
  87.           struct( ELEMENT_LIST );
  88.  
  89.       The first and    second forms explicitly    identify the name of
  90.       the class being created.  The    third form assumes the current
  91.       package name as the class name.
  92.  
  93.       An object of a class created by the first and    third forms is
  94.       based    on an array, whereas an    object of a class created by
  95.       the second form is based on a    hash. The array-based forms
  96.       will be somewhat faster and smaller; the hash-based forms
  97.       are more flexible.
  98.  
  99.       The class created by struct must not be a subclass of
  100.       another class    other than UNIVERSAL.
  101.  
  102.       A function named new must not    be explicitly defined in a
  103.       class    created    by struct.
  104.  
  105.       The _E_L_E_M_E_N_T__L_I_S_T has the form
  106.  
  107.           NAME => TYPE, ...
  108.  
  109.       Each name-type pair declares one element of the struct. Each
  110.       element name will be defined as an accessor method unless a
  111.       method by that name is explicitly defined; in    the latter
  112.       case,    a warning is issued if the warning flag    (----wwww) is    set.
  113.  
  114.       EEEElllleeeemmmmeeeennnntttt TTTTyyyyppppeeeessss    aaaannnndddd AAAAcccccccceeeessssssssoooorrrr MMMMeeeetttthhhhooooddddssss
  115.  
  116.       The four element types -- scalar, array, hash, and class --
  117.       are represented by strings --    '$', '@', '%', and a class
  118.       name -- optionally preceded by a '*'.
  119.  
  120.       The accessor method provided by struct for an    element
  121.       depends on the declared type of the element.
  122.  
  123.       Scalar ('$' or '*$')
  124.            The element is a    scalar,    and is initialized to undef.
  125.  
  126.  
  127.  
  128.  
  129.      Page 2                        (printed 10/23/98)
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.      CCCCllllaaaassssssss::::::::SSSSttttrrrruuuucccctttt((((3333)))) 1111////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222)))) CCCCllllaaaassssssss::::::::SSSSttttrrrruuuucccctttt((((3333))))
  137.  
  138.  
  139.  
  140.            The accessor's argument,    if any,    is assigned to the
  141.            element.
  142.  
  143.            If the element type is '$', the value of    the element
  144.            (after assignment) is returned. If the element type is
  145.            '*$', a reference to the    element    is returned.
  146.  
  147.       Array    ('@' or    '*@')
  148.            The element is an array,    initialized to ().
  149.  
  150.            With no argument, the accessor returns a    reference to
  151.            the element's whole array.
  152.  
  153.            With one    or two arguments, the first argument is    an
  154.            index specifying    one element of the array; the second
  155.            argument, if present, is    assigned to the    array element.
  156.            If the element type is '@', the accessor    returns    the
  157.            array element value.  If    the element type is '*@', a
  158.            reference to the    array element is returned.
  159.  
  160.       Hash ('%' or '*%')
  161.            The element is a    hash, initialized to ().
  162.  
  163.            With no argument, the accessor returns a    reference to
  164.            the element's whole hash.
  165.  
  166.            With one    or two arguments, the first argument is    a key
  167.            specifying one element of the hash; the second
  168.            argument, if present, is    assigned to the    hash element.
  169.            If the element type is '%', the accessor    returns    the
  170.            hash element value.  If the element type    is '*%', a
  171.            reference to the    hash element is    returned.
  172.  
  173.       Class    ('Class_Name' or '*Class_Name')
  174.            The element's value must    be a reference blessed to the
  175.            named class or to one of    its subclasses.    The element is
  176.            initialized to the result of calling the    new
  177.            constructor of the named    class.
  178.  
  179.            The accessor's argument,    if any,    is assigned to the
  180.            element.    The accessor will croak    if this    is not an
  181.            appropriate object reference.
  182.  
  183.            If the element type does    not start with a '*', the
  184.            accessor    returns    the element value (after assignment).
  185.            If the element type starts with a '*', a    reference to
  186.            the element itself is returned.
  187.  
  188.      EEEEXXXXAAAAMMMMPPPPLLLLEEEESSSS
  189.       Example 1
  190.            Giving a    struct element a class type that is also a
  191.            struct is how structs are nested.  Here,    timeval
  192.  
  193.  
  194.  
  195.      Page 3                        (printed 10/23/98)
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.      CCCCllllaaaassssssss::::::::SSSSttttrrrruuuucccctttt((((3333)))) 1111////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222)))) CCCCllllaaaassssssss::::::::SSSSttttrrrruuuucccctttt((((3333))))
  203.  
  204.  
  205.  
  206.            represents a time (seconds and microseconds), and
  207.            rusage has two elements,    each of    which is of type
  208.            timeval.
  209.  
  210.            use Class::Struct;
  211.  
  212.            struct( rusage => {
  213.                ru_utime    => timeval,  # seconds
  214.                ru_stime    => timeval,  # microseconds
  215.            });
  216.  
  217.            struct( timeval => [
  218.                tv_secs    => '$',
  219.                tv_usecs    => '$',
  220.            ]);
  221.  
  222.                # create    an object:
  223.            my $t = new rusage;
  224.                # $t->ru_utime and $t->ru_stime are objects of type timeval.
  225.  
  226.                # set $t->ru_utime to 100.0 sec and $t->ru_stime    to 5.0 sec.
  227.            $t->ru_utime->tv_secs(100);
  228.            $t->ru_utime->tv_usecs(0);
  229.            $t->ru_stime->tv_secs(5);
  230.            $t->ru_stime->tv_usecs(0);
  231.  
  232.  
  233.       Example 2
  234.            An accessor function can    be redefined in    order to
  235.            provide additional checking of values, etc.  Here, we
  236.            want the    count element always to    be nonnegative,    so we
  237.            redefine    the count accessor accordingly.
  238.  
  239.            package MyObj;
  240.            use Class::Struct;
  241.  
  242.                    # declare the struct
  243.            struct ( 'MyObj', { count =>    '$', stuff => '%' } );
  244.  
  245.                    # override the default accessor method for 'count'
  246.            sub count {
  247.                my $self    = shift;
  248.                if ( @_ ) {
  249.                die 'count must be nonnegative' if $_[0] < 0;
  250.                $self->{'count'} = shift;
  251.                warn    "Too many args to count" if @_;
  252.                }
  253.                return $self->{'count'};
  254.            }
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.      Page 4                        (printed 10/23/98)
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.      CCCCllllaaaassssssss::::::::SSSSttttrrrruuuucccctttt((((3333)))) 1111////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222)))) CCCCllllaaaassssssss::::::::SSSSttttrrrruuuucccctttt((((3333))))
  269.  
  270.  
  271.  
  272.            package main;
  273.            $x =    new MyObj;
  274.            print "\$x->count(5)    = ", $x->count(5), "\n";
  275.                        # prints '$x->count(5) = 5'
  276.  
  277.            print "\$x->count = ", $x->count, "\n";
  278.                        # prints '$x->count = 5'
  279.  
  280.            print "\$x->count(-5) = ", $x->count(-5), "\n";
  281.                        # dies due to negative argument!
  282.  
  283.  
  284.      AAAAuuuutttthhhhoooorrrr aaaannnndddd    MMMMooooddddiiiiffffiiiiccccaaaattttiiiioooonnnn HHHHiiiissssttttoooorrrryyyy
  285.       Renamed to Class::Struct and modified    by Jim Miner, 1997-
  286.       04-02.
  287.  
  288.           members()    function removed.
  289.           Documentation corrected and extended.
  290.           Use of struct() in a subclass prohibited.
  291.           User definition of accessor allowed.
  292.           Treatment    of '*' in element types    corrected.
  293.           Treatment    of classes as element types corrected.
  294.           Class name to struct() made optional.
  295.           Diagnostic checks    added.
  296.  
  297.       Originally Class::Template by    Dean Roehrich.
  298.  
  299.           #    Template.pm   --- struct/member    template builder
  300.           #      12mar95
  301.           #      Dean Roehrich
  302.           #
  303.           #    changes/bugs fixed since 28nov94 version:
  304.           #     - podified
  305.           #    changes/bugs fixed since 21nov94 version:
  306.           #     - Fixed examples.
  307.           #    changes/bugs fixed since 02sep94 version:
  308.           #     - Moved to Class::Template.
  309.           #    changes/bugs fixed since 20feb94 version:
  310.           #     - Updated to be a more    proper module.
  311.           #     - Added "use strict".
  312.           #     - Bug in build_methods, was using @var    when @$var needed.
  313.           #     - Now using my() rather than local().
  314.           #
  315.           #    Uses perl5 classes to create nested data types.
  316.           #    This is    offered    as one implementation of Tom Christiansen's "structs.pl"
  317.           #    idea.
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.      Page 5                        (printed 10/23/98)
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.      CCCCllllaaaassssssss::::::::SSSSttttrrrruuuucccctttt((((3333)))) 1111////AAAAuuuugggg////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222)))) CCCCllllaaaassssssss::::::::SSSSttttrrrruuuucccctttt((((3333))))
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.      Page 6                        (printed 10/23/98)
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.